/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelMoverProduto.java
 *
 * Created on 23/09/2011, 08:19:19
 */
package modules.enderecamento.gui.jpanel;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import modules.enderecamento.exception.EspacoExcedidoPaleteException;
import modules.enderecamento.exception.EspacoExcedidoRuaException;
import modules.enderecamento.exception.SaldoInsuficienteException;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.gui.custom.LimitedIntegerJTextField;
import modules.enderecamento.models.dto.Enderecamento;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.models.dto.Produto;
import modules.enderecamento.models.dto.Rua;
import modules.enderecamento.util.BoxPanel;
import modules.enderecamento.util.Funcoes;
import modules.enderecamento.validation.Validation;

/**
 *
 * @author Clezio
 */
public class PanelMoverProduto extends javax.swing.JPanel {

    private WMSFacade wmsFacade;

    /** Creates new form PanelMoverProduto */
    public PanelMoverProduto() {
        initComponents();
        wmsFacade = new WMSFacade();
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(93);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(303);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(26);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfPaleteDestino = new LimitedIntegerJTextField(10);
        lbPaleteDestino = new modules.enderecamento.gui.custom.WMSLabel();
        btnTransferir = new modules.enderecamento.gui.custom.WMSJButton();
        lbPaleteOrigem = new modules.enderecamento.gui.custom.WMSLabel();
        tfPaleteOrigem = new LimitedIntegerJTextField(10);
        lbCodigo = new modules.enderecamento.gui.custom.WMSLabel();
        tfCodigo = new modules.enderecamento.gui.custom.WMSJTextField();
        tfDescricao = new modules.enderecamento.gui.custom.WMSJTextField();
        tfQuantidadeOrigem = new modules.enderecamento.gui.custom.WMSJTextField();
        lbQuantidade = new modules.enderecamento.gui.custom.WMSLabel();
        tfQuantidadeTranferencia = new LimitedIntegerJTextField(10);
        lbRua = new modules.enderecamento.gui.custom.WMSLabel();
        tfRuaDestino = new modules.enderecamento.gui.custom.WMSJTextField();
        tfConfirmar = new modules.enderecamento.gui.custom.WMSJTextField();
        wMSLabel6 = new modules.enderecamento.gui.custom.WMSLabel();
        lbConfirmar = new modules.enderecamento.gui.custom.WMSLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setBackground(new java.awt.Color(238, 238, 238));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Tranferência de Produtos do Palete");

        tfPaleteDestino.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPaleteDestinoFocusLost(evt);
            }
        });
        tfPaleteDestino.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPaleteDestinoKeyPressed(evt);
            }
        });

        lbPaleteDestino.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPaleteDestino.setText("Palete (Destino):");

        btnTransferir.setMnemonic('T');
        btnTransferir.setText("Transferir");
        btnTransferir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransferirActionPerformed(evt);
            }
        });
        btnTransferir.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnTransferirKeyPressed(evt);
            }
        });

        lbPaleteOrigem.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPaleteOrigem.setText("Palete (Origem):");

        tfPaleteOrigem.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPaleteOrigemFocusLost(evt);
            }
        });
        tfPaleteOrigem.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPaleteOrigemKeyPressed(evt);
            }
        });

        lbCodigo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCodigo.setText("Código:");

        tfCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfCodigoFocusLost(evt);
            }
        });
        tfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfCodigoKeyPressed(evt);
            }
        });

        tfDescricao.setEditable(false);
        tfDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfDescricaoKeyPressed(evt);
            }
        });

        tfQuantidadeOrigem.setEditable(false);

        lbQuantidade.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbQuantidade.setText("Quantidade:");

        tfQuantidadeTranferencia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfQuantidadeTranferenciaKeyPressed(evt);
            }
        });

        lbRua.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbRua.setText("Rua:");

        tfRuaDestino.setEditable(false);
        tfRuaDestino.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfRuaDestinoFocusLost(evt);
            }
        });
        tfRuaDestino.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfRuaDestinoKeyPressed(evt);
            }
        });

        tfConfirmar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConfirmarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfConfirmarKeyReleased(evt);
            }
        });

        wMSLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        wMSLabel6.setText("(S) ou (N)");

        lbConfirmar.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbConfirmar.setText("Confimar dados:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Descrição", "Qtde"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lbPaleteOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfPaleteOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lbCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(tfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(tfQuantidadeOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lbQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfQuantidadeTranferencia, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lbPaleteDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfPaleteDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(lbConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(wMSLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(lbRua, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(tfRuaDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(132, 132, 132)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 556, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnTransferir, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPaleteOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPaleteOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfQuantidadeOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfQuantidadeTranferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPaleteDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPaleteDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(lbRua, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tfRuaDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lbConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tfConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(wMSLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(7, 7, 7)
                .addComponent(btnTransferir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(78, 78, 78))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfPaleteDestinoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPaleteDestinoKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if(tfPaleteDestino.getText().length() > 0){
                tfRuaDestino.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfPaleteDestino.setText("");
            tfRuaDestino.setText("");
            tfQuantidadeTranferencia.requestFocus();
        }
}//GEN-LAST:event_tfPaleteDestinoKeyPressed

    private void btnTransferirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransferirActionPerformed
        efetuarTransferencia();
}//GEN-LAST:event_btnTransferirActionPerformed

    private void btnTransferirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnTransferirKeyPressed
}//GEN-LAST:event_btnTransferirKeyPressed

    private void tfPaleteOrigemKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPaleteOrigemKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if (tfPaleteOrigem.getText().length() > 0) {
                tfQuantidadeOrigem.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfPaleteOrigem.setText("");
        }
    }//GEN-LAST:event_tfPaleteOrigemKeyPressed

    private void tfCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfCodigoFocusLost
        if(tfCodigo.getText().length() > 0){
            consultarProdutosPalete(tfCodigo.getText());
        }else{
            tfDescricao.setText("");
            tfQuantidadeOrigem.setText("");
        }
}//GEN-LAST:event_tfCodigoFocusLost

    private void tfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCodigoKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if(tfCodigo.getText().length() > 0){
                tfQuantidadeTranferencia.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfCodigo.setText("");
            tfPaleteOrigem.requestFocus();
        }
}//GEN-LAST:event_tfCodigoKeyPressed

    private void tfDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfDescricaoKeyPressed
        // TODO add your handling code here:
}//GEN-LAST:event_tfDescricaoKeyPressed

    private void tfQuantidadeTranferenciaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfQuantidadeTranferenciaKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            tfPaleteDestino.requestFocus();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfQuantidadeTranferencia.setText("");
            tfCodigo.requestFocus();
        }
    }//GEN-LAST:event_tfQuantidadeTranferenciaKeyPressed

    private void tfPaleteOrigemFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPaleteOrigemFocusLost
        if(tfPaleteOrigem.getText().length() > 0){
            consultarProdutosPalete(null);
        }
    }//GEN-LAST:event_tfPaleteOrigemFocusLost

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        tfPaleteOrigem.requestFocus();
    }//GEN-LAST:event_formAncestorAdded

    private void tfRuaDestinoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfRuaDestinoFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_tfRuaDestinoFocusLost

    private void tfRuaDestinoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfRuaDestinoKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            tfConfirmar.requestFocus();
            if (tfRuaDestino.getText().length() > 0) {
                Rua rua = wmsFacade.obterRuaByName(tfRuaDestino.getText());
                if (rua == null) {
                    BoxPanel.showMessageDialog(null, "Rua não encontrada");
                    tfRuaDestino.setText("");
                    tfRuaDestino.requestFocus();
                }
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfRuaDestino.setText("");
            tfPaleteDestino.requestFocus();
        }

    }//GEN-LAST:event_tfRuaDestinoKeyPressed

    private void tfConfirmarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConfirmarKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            String confirm = tfConfirmar.getText();
            if (confirm.toUpperCase().equals("S")) {
                efetuarTransferencia();
            } else if (confirm.toUpperCase().equals("N")) {
                clearFields();
            } else {
                BoxPanel.showMessageDialog(null, "A confirmação é um campo obrigatório.");
                tfConfirmar.setText("");
                tfConfirmar.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfConfirmar.setText("");
            if(tfRuaDestino.isEditable()){
                tfRuaDestino.requestFocus();
            }else{
                tfPaleteDestino.requestFocus();
            }
        }
}//GEN-LAST:event_tfConfirmarKeyPressed

    private void tfConfirmarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConfirmarKeyReleased
        if (tfConfirmar.getText().length() > 0) {
            tfConfirmar.setText(tfConfirmar.getText().substring(0, 1));
        }
}//GEN-LAST:event_tfConfirmarKeyReleased

    private void tfPaleteDestinoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPaleteDestinoFocusLost
        tfRuaDestino.setEditable(false);
        if(tfPaleteDestino.getText().length() > 0){
            Palete palete = wmsFacade.obterPaleteById(Integer.parseInt(tfPaleteDestino.getText()));
            if(palete != null){
                if(palete.getRua() != null){
                    tfRuaDestino.setText(palete.getRua().getNome());
                    tfConfirmar.requestFocus();
                }else{
                    tfRuaDestino.setEditable(true);
                    tfRuaDestino.setText("");
                    tfRuaDestino.requestFocus();
                }
            }else{
                BoxPanel.showMessageDialog(null, "Palete não encontrado.");
                tfPaleteDestino.setText("");
                tfRuaDestino.setText("");
                tfPaleteDestino.requestFocus();
            }
        }
    }//GEN-LAST:event_tfPaleteDestinoFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnTransferir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private modules.enderecamento.gui.custom.WMSLabel lbCodigo;
    private modules.enderecamento.gui.custom.WMSLabel lbConfirmar;
    private modules.enderecamento.gui.custom.WMSLabel lbPaleteDestino;
    private modules.enderecamento.gui.custom.WMSLabel lbPaleteOrigem;
    private modules.enderecamento.gui.custom.WMSLabel lbQuantidade;
    private modules.enderecamento.gui.custom.WMSLabel lbRua;
    private modules.enderecamento.gui.custom.WMSJTextField tfCodigo;
    private modules.enderecamento.gui.custom.WMSJTextField tfConfirmar;
    private modules.enderecamento.gui.custom.WMSJTextField tfDescricao;
    private modules.enderecamento.gui.custom.WMSJTextField tfPaleteDestino;
    private modules.enderecamento.gui.custom.WMSJTextField tfPaleteOrigem;
    private modules.enderecamento.gui.custom.WMSJTextField tfQuantidadeOrigem;
    private modules.enderecamento.gui.custom.WMSJTextField tfQuantidadeTranferencia;
    private modules.enderecamento.gui.custom.WMSJTextField tfRuaDestino;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel6;
    // End of variables declaration//GEN-END:variables

    private void efetuarTransferencia() {
        Palete paleteOrigem = null;
        Palete paleteDestino = null;
        Rua ruaDestino = null;
        Produto produto = null;
        if(tfPaleteOrigem.getText().length() == 0){
            Validation.getInstance().addErros("paleteOrigem", "Palete de origem é um campo obrigatório.");
        }
        if(tfPaleteDestino.getText().length() == 0){
            Validation.getInstance().addErros("paleteOrigem", "Palete de destino é um campo obrigatório.");
        }
        if(tfCodigo.getText().length() == 0){
            Validation.getInstance().addErros("codigo", "Código é um campo obrigatório.");
        }
        if(tfQuantidadeTranferencia.getText().length() == 0){
            Validation.getInstance().addErros("quantidadeTransferencia", "Quantidade é um campo obrigatório.");
        }
        if(tfRuaDestino.getText().length() == 0){
            Validation.getInstance().addErros("ruaDestino", "Rua é um campo obrigatório.");
        }
        if(tfPaleteOrigem.getText().equals(tfPaleteDestino.getText()) && tfPaleteDestino.getText().length() > 0){
            Validation.getInstance().addErros("paleteIgual", "O Palete de destino não pode ser o igual ao de origem.");
        }
        
        if(Validation.getInstance().hasErrors()){
            BoxPanel.showMessageError(Funcoes.getListErros(Validation.getInstance().getErros()));
            return;
        }

        paleteOrigem = wmsFacade.obterPaleteById(Integer.parseInt(tfPaleteOrigem.getText()));
        paleteDestino = wmsFacade.obterPaleteById(Integer.parseInt(tfPaleteDestino.getText()));
        ruaDestino = wmsFacade.obterRuaByName(tfRuaDestino.getText());
        produto = wmsFacade.obterProdutoByCodigo(tfCodigo.getText());

        if(paleteOrigem == null){
            BoxPanel.showMessageDialog(null, "Palete de origem não encontrado.");
            return;
        }
        if(paleteDestino == null){
            BoxPanel.showMessageDialog(null, "Palete de destino não encontrado.");
            return;
        }
        if(produto == null){
            BoxPanel.showMessageDialog(null, "Produto não encontrado.");
            return;
        }
        if(ruaDestino == null){
            BoxPanel.showMessageDialog(null, "Rua de destino não encontrada.");
            return;
        }

        if(paleteOrigem!= null && paleteDestino != null && ruaDestino != null && produto != null){
            Integer quantidade = Integer.parseInt(tfQuantidadeTranferencia.getText());
            paleteDestino.setRua(ruaDestino);
            try {
                if (wmsFacade.moverProdutoPalete(paleteOrigem, paleteDestino, produto, quantidade)) {
                    BoxPanel.showMessageDialog(null, "Operaçao realizada com sucesso.");
                    clearFields();
                    DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                    modelo.setNumRows(0);
                }
            } catch (SaldoInsuficienteException ex) {
                BoxPanel.showMessageDialog(null, ex.getMessage());
            } catch (EspacoExcedidoPaleteException ex) {
                BoxPanel.showMessageDialog(null, ex.getMessage());
            } catch (EspacoExcedidoRuaException ex) {
                BoxPanel.showMessageDialog(null, ex.getMessage());
            }
        }
    }

    private void consultarProdutosPalete(String codigo) {
        tfDescricao.setText("");
        tfQuantidadeOrigem.setText("");
        if (tfPaleteOrigem.getText().length() > 0) {
            List<Enderecamento> endecamentos = wmsFacade.obterEnderecamento("from Enderecamento where palete_id = " + tfPaleteOrigem.getText());
            if (endecamentos != null) {
                DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                modelo.setNumRows(0);
                if (endecamentos.isEmpty()) {
                    Palete palete = wmsFacade.obterPaleteById(Integer.parseInt(tfPaleteOrigem.getText()));
                    if (palete == null) {
                        BoxPanel.showMessageDialog(null, "Palete não encontrado.");
                    } else {
                        BoxPanel.showMessageDialog(null, "O Palete se encontra vazio.");
                    }
                    tfPaleteOrigem.setText("");
                    tfPaleteOrigem.requestFocus();
                }else{
                    if(codigo != null){
                        tfQuantidadeTranferencia.requestFocus();
                    }else{
                        tfCodigo.requestFocus();
                    }
                    
                    boolean selectProduto = false;

                    for (int i = 0; i < endecamentos.size(); i++) {
                        Produto produto = endecamentos.get(i).getProduto();
                        Integer qtde = endecamentos.get(i).getQuantidade();
                        if(codigo != null && codigo.equals(produto.getCodigo())){
                            tfDescricao.setText(produto.getDescricao());
                            tfQuantidadeOrigem.setText(""+qtde);
                            selectProduto = true;
                        }
                        modelo.addRow(new Object[]{
                                    produto.getCodigo(), produto.getDescricao(), ""+qtde
                                });
                    }
                    if(codigo != null && !selectProduto){
                        if(codigo.length() > 0){
                            BoxPanel.showMessageDialog(null, "Produto não encontrado no Palete informado.");
                            tfCodigo.setText("");
                            tfCodigo.requestFocus();
                        }
                    }
                }
            }
        } else {
            BoxPanel.showMessageDialog(null, "Informe o Palete");
        }
    }

    private void clearFields() {
        tfCodigo.setText("");
        tfDescricao.setText("");
        tfQuantidadeTranferencia.setText("");
        tfQuantidadeOrigem.setText("");
        tfPaleteOrigem.setText("");
        tfPaleteDestino.setText("");
        tfConfirmar.setText("");
        tfRuaDestino.setText("");
        tfRuaDestino.setEditable(false);
        tfPaleteOrigem.requestFocus();
    }
}
